import 'package:flutter/material.dart';
import 'dart:async';

class Layer {
  final Widget child;
  final BuildContext context;
  final int millis;
  final Color bgcolor;

  Layer({
    this.child,
    @required this.context,
    this.millis = 2000,
    this.bgcolor
  });


  void _toastBasic(Widget widget) async{
    /// Overlay相当于浮层上下文
    OverlayState overlayState = Overlay.of(context);
    OverlayEntry overlayEntry = OverlayEntry(builder: (BuildContext context) => widget);
    /// 将浮层插入上下文
    overlayState.insert(overlayEntry);
    await Future.delayed(Duration(milliseconds: millis));
    /// 移除浮层
    overlayEntry.remove();
  }

  void showToast() {
     Widget widget = Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      color: this.bgcolor,
      child: this.child
    );
    _toastBasic(widget);
  }
}